package com.xgc.erp.info.payment.ctrl;

import com.jfinal.aop.Inject;
import com.jfinal.plugin.activerecord.Record;
import com.xgc.common.base.BaseController;
import com.xgc.common.config.Constan;
import com.xgc.common.model.PaymentWay;
import com.xgc.common.visit.Visitor;
import com.xgc.common.vo.Feedback;
import com.xgc.erp.info.payment.service.PaymentWayService;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Date;

//@ControllerBind(path="/erp/info/payment")
public class PaymentWayController extends BaseController {
    @Inject
    PaymentWayService service;

    public void index() {
        render("index.html");
    }

    public void list(){
        Record record = new Record();
        renderJson(service.queryForList(getParaToInt("pageNumber",1), getParaToInt("pageSize",10),record,"order by create_time desc"));
    }

    public void add() {
        render("add.html");
    }

    public void save() {
        PaymentWay paymentWay=getBean(PaymentWay.class);
        Integer wayId = service.getPrimaryKey(Constan.SEQ_PAYMENT_WAY);
        paymentWay.setWayId(wayId);
        Visitor vs = getVisitor();
        paymentWay.setCreator(vs.getCode());
        paymentWay.setCreateTime(new Date());
        boolean flag = service.isExit(service.getPK(),paymentWay.toString());
        if(flag){
            setException("主键已存在");
        }else {
            flag = service.save(paymentWay);
            if(!flag) {
                setException("保存失败");
            }
        }
        render("add.html");
    }

    public void edit() throws UnsupportedEncodingException {
        PaymentWay paymentWay =(PaymentWay) service.findById(URLDecoder.decode(getPara("primaryKey"),"utf-8"));
        setAttr("paymentWay", paymentWay);
        render("edit.html");
    }

    public void update() {
        PaymentWay paymentWay=getBean(PaymentWay.class);
        paymentWay.setModifier(getVisitor().getCode());
        paymentWay.setUpdateTime(new Date());
        service.update(paymentWay);
        setAttr("paymentWay", paymentWay);
        render("edit.html");
    }

    public void delete() {
        service.deleteByIds(getIds());
        renderJson(Feedback.success());
    }

}
